# -*- coding:utf-8 -*-
# 列表推导式

squares = []
for x in range(10) :
    squares.append(x**2)

print(x,squares) # 9 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

squares2 = [x2 for x2 in range(10)]
print(squares2)


list = []

resList = [ (x,y) for x in [1,2,3] for y in [2,3,4] if x!=y ]
print(resList) # [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 2), (3, 4)]


# resList2 = [x,x*2 for x in range(3)] # invalid syntax

vec = [[1,2,3], [4,5,6], [7,8,9]]
resList3 = [num for elem in vec for num in elem]
print(resList3) # [1, 2, 3, 4, 5, 6, 7, 8, 9]

#
matrix = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
]

# 行列交换
resList4 = [[row[i] for row in matrix] for i in range(4)]
print(resList4) # [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
